package com.starmark.gateway.springcloud.provider.swagger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;

import java.util.Optional;


@Controller
@RequestMapping()
public class SwaggerHandler {
    @Autowired(required = false)
    private SecurityConfiguration securityConfiguration;
    @Autowired(required = false)
    private UiConfiguration uiConfiguration;
    private final SwaggerProvider swaggerResources;

    @Autowired
    public SwaggerHandler(SwaggerProvider swaggerResources) {
        this.swaggerResources = swaggerResources;
    }


    @GetMapping("/{projectCode}/swagger-resources/configuration/security")
    @ResponseBody
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration(@PathVariable("projectCode") String projectCode) {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/{projectCode}/swagger-resources/configuration/ui")
    @ResponseBody
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration(){
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/{projectCode}/swagger-resources")
    @ResponseBody
    public Mono<ResponseEntity> swaggerResources(@PathVariable("projectCode") String projectCode) {
        return Mono.just((new ResponseEntity<>(swaggerResources.get(projectCode), HttpStatus.OK)));
    }



}


